# Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
# This source file is part of the Cangjie project, licensed under Apache-2.0
# with Runtime Library Exception.
#
# See https://cangjie-lang.cn/pages/LICENSE for license information.

cmake_minimum_required(VERSION 3.14.1)

project(cjthread C CXX)

if (${CMAKE_BUILD_TYPE} STREQUAL "Debug")
    add_definitions(-DCJTHREAD_DEBUG=1)
    add_definitions(-DMRT_DEBUG=1)
endif ()

if(${TARGET} STREQUAL "linux_x86_64")
    set(CMAKE_SYSTEM_NAME "Linux")
    set(CMAKE_SYSTEM_PROCESSOR "x86_64")
    include(../../build/cmake/toolchain/linux_x86_64.cmake)
    ADD_DEFINITIONS(-DTRACE_FOLDER_PREFIX="")
elseif(${TARGET} STREQUAL "linux_x86_64_gcov")
    set(CMAKE_SYSTEM_NAME "Linux")
    set(CMAKE_SYSTEM_PROCESSOR "x86_64")
    include(../../build/cmake/toolchain/linux_x86_64_gcov.cmake)
    ADD_DEFINITIONS(-DTRACE_FOLDER_PREFIX="")
elseif(${TARGET} STREQUAL "linux_x86_64_fuzz")
    set(CMAKE_SYSTEM_NAME "Linux")
    set(CMAKE_SYSTEM_PROCESSOR "x86_64")
    include(../../build/cmake/toolchain/linux_x86_64_fuzz.cmake)
    ADD_DEFINITIONS(-DTRACE_FOLDER_PREFIX="")
elseif(${TARGET} STREQUAL "linux_x86_64_cangjie")
    set(CMAKE_SYSTEM_NAME "Linux")
    set(CMAKE_SYSTEM_PROCESSOR "x86_64")
    include(../../build/cmake/toolchain/linux_x86_64_cangjie.cmake)
    ADD_DEFINITIONS(-DTRACE_FOLDER_PREFIX="linux_x86_64")
elseif(${TARGET} STREQUAL "linux_aarch64")
    set(CMAKE_SYSTEM_NAME "Linux")
    set(CMAKE_SYSTEM_PROCESSOR "aarch64")
    include(../../build/cmake/toolchain/linux_aarch64.cmake)
    ADD_DEFINITIONS(-DTRACE_FOLDER_PREFIX="")
elseif(${TARGET} STREQUAL "linux_aarch64_cangjie")
    set(CMAKE_SYSTEM_NAME "Linux")
    set(CMAKE_SYSTEM_PROCESSOR "aarch64")
    include(../../build/cmake/toolchain/linux_aarch64_cangjie.cmake)
    ADD_DEFINITIONS(-DTRACE_FOLDER_PREFIX="linux_aarch64")
elseif(${TARGET} STREQUAL "ohos_aarch64_cangjie")
    set(CMAKE_SYSTEM_NAME "Linux")
    set(CMAKE_SYSTEM_PROCESSOR "aarch64")
    include(../../build/cmake/toolchain/ohos_aarch64_cangjie.cmake)
    ADD_DEFINITIONS(-DTRACE_FOLDER_PREFIX="linux_ohos_aarch64")
elseif(${TARGET} STREQUAL "ohos_x86_64_cangjie")
    set(CMAKE_SYSTEM_NAME "Linux")
    set(CMAKE_SYSTEM_PROCESSOR "x86_64")
    include(../../build/cmake/toolchain/ohos_x86_64_cangjie.cmake)
    ADD_DEFINITIONS(-DTRACE_FOLDER_PREFIX="linux_ohos_x86_64")
elseif(${TARGET} STREQUAL "hos_aarch64_cangjie")
    set(CMAKE_SYSTEM_NAME "Linux")
    set(CMAKE_SYSTEM_PROCESSOR "aarch64")
    include(../../build/cmake/toolchain/config.cmake)
    include(../../build/cmake/toolchain/${TARGET}.cmake)
    ADD_DEFINITIONS(-DTRACE_FOLDER_PREFIX="linux_hos_aarch64")
elseif(${TARGET} STREQUAL "windows_x86_64")
    set(CMAKE_SYSTEM_NAME "Windows")
    set(CMAKE_SYSTEM_PROCESSOR "x86_64")
    include(../../build/cmake/toolchain/windows_x86_64.cmake)
    ADD_DEFINITIONS(-DTRACE_FOLDER_PREFIX="windows_x86_64")
elseif(${TARGET} STREQUAL "cross_windows_x86_64")
    set(CMAKE_SYSTEM_NAME "Windows")
    set(CMAKE_SYSTEM_PROCESSOR "x86_64")
    include(../../build/cmake/toolchain/cross_windows_x86_64.cmake)
    ADD_DEFINITIONS(-DTRACE_FOLDER_PREFIX="windows_x86_64")
elseif(${TARGET} STREQUAL "macos_x86_64")
    set(CMAKE_SYSTEM_NAME "Darwin")
    set(CMAKE_SYSTEM_PROCESSOR "x86_64")
    include(../../build/cmake/toolchain/macos_x86_64.cmake)
    ADD_DEFINITIONS(-DTRACE_FOLDER_PREFIX="macos_x86_64")
elseif(${TARGET} STREQUAL "macos_aarch64")
    set(CMAKE_SYSTEM_NAME "Darwin")
    set(CMAKE_SYSTEM_PROCESSOR "aarch64")
    include(../../build/cmake/toolchain/macos_aarch64.cmake)
    ADD_DEFINITIONS(-DTRACE_FOLDER_PREFIX="macos_aarch64")
endif()
include_directories(${PROJECT_SOURCE_DIR}/../../src)
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
    include_directories(${PROJECT_SOURCE_DIR}/../../src/os/Windows)
endif()
include_directories(${PROJECT_SOURCE_DIR}/../../third_party/third_party_bounds_checking_function/include)

if ("${BUILDING_STAGE}" STREQUAL "cjasan")
    add_compile_definitions(CANGJIE_ASAN_SUPPORT)
    add_compile_definitions(CANGJIE_SANITIZER_SUPPORT)
    add_compile_definitions(GENERAL_ASAN_SUPPORT_INTERFACE)
elseif ("${BUILDING_STAGE}" STREQUAL "cjtsan")
    add_compile_definitions(CANGJIE_TSAN_SUPPORT)
    add_compile_definitions(CANGJIE_SANITIZER_SUPPORT)
elseif ("${BUILDING_STAGE}" STREQUAL "cjhwasan")
    add_compile_definitions(CANGJIE_HWASAN_SUPPORT)
    add_compile_definitions(CANGJIE_SANITIZER_SUPPORT)
    add_compile_definitions(GENERAL_ASAN_SUPPORT_INTERFACE)
endif ()

if (RUNTIME_BACKWARD_PTRAUTH_CFI MATCHES 1)
    add_compile_definitions(ENABLE_BACKWARD_PTRAUTH_CFI)
endif ()

if (RUNTIME_FORWARD_PTRAUTH_CFI MATCHES 1)
    add_compile_definitions(ENABLE_FORWARD_PTRAUTH_CFI)
endif ()

ADD_SUBDIRECTORY(src)
